package com.example.model;

import java.sql.*;
import java.util.*;
import com.example.util.DBUtil;

public class CartDAO {
    // 添加到购物车
    public static boolean addToCart(int userId, int bookId, int quantity) {
        String sql = "INSERT INTO cart (user_id, book_id, quantity) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE quantity = quantity + ?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            ps.setInt(2, bookId);
            ps.setInt(3, quantity);
            ps.setInt(4, quantity);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 查询用户购物车
    public static List<Map<String, Object>> getCartByUser(int userId) {
        List<Map<String, Object>> list = new ArrayList<>();
        String sql = "SELECT c.id, c.book_id, c.quantity, b.name, b.cover_url, b.price FROM cart c JOIN books b ON c.book_id = b.id WHERE c.user_id = ?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Map<String, Object> item = new HashMap<>();
                item.put("id", rs.getInt("id"));
                item.put("book_id", rs.getInt("book_id"));
                item.put("quantity", rs.getInt("quantity"));
                item.put("title", rs.getString("name"));
                item.put("cover_url", rs.getString("cover_url"));
                item.put("price", rs.getBigDecimal("price"));
                list.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    // 修改购物车数量
    public static boolean updateQuantity(int userId, int bookId, int quantity) {
        String sql = "UPDATE cart SET quantity = ? WHERE user_id = ? AND book_id = ?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, quantity);
            ps.setInt(2, userId);
            ps.setInt(3, bookId);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除购物车某项
    public static boolean deleteItem(int userId, int bookId) {
        String sql = "DELETE FROM cart WHERE user_id = ? AND book_id = ?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            ps.setInt(2, bookId);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 清空购物车
    public static boolean clearCart(int userId) {
        String sql = "DELETE FROM cart WHERE user_id = ?";
        try (Connection conn = DBUtil.getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1, userId);
            return ps.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
} 